//
//  XWJokeViewController.m
//  CGWSWW
//
//  Created by lanou3g on 16/8/12.
//  Copyright © 2016年 CXG. All rights reserved.
//

#import "XWJokeViewController.h"
#import "HeadHelp.h"
#import "XWInterestViewController.h"
#import "XWFunnyViewController.h"
#define KScrollViewWidth self.scrollView.bounds.size.width
#define KScrollViewHeight self.scrollView.bounds.size.height
@interface XWJokeViewController ()<UIScrollViewDelegate>
/**  UIScrollView */
@property (weak,nonatomic) UIScrollView *scrollView;

/** UISegmentedControl */
@property (strong,nonatomic) UISegmentedControl *seg;

@end

@implementation XWJokeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建ScrollView
    [self setupScrollView];
    //添加子控制器
    [self setupChildViewControllers];
    //创建控件
    [self setUI];

}

- (void)setupChildViewControllers{
    XWInterestViewController *interest = [[XWInterestViewController alloc] init];
    [self addChildViewController:interest];
    XWFunnyViewController *funny = [[XWFunnyViewController alloc] init];
    [self addChildViewController:funny];
}
-(void)setupScrollView{
    //不允许自动调整scrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *scrollView =[[UIScrollView alloc] init];

    scrollView.backgroundColor = XWRandomColor;
    [self.view addSubview:scrollView];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
    }];
    self.scrollView = scrollView;

    scrollView.backgroundColor = XWCommonColor;
    scrollView.frame = CGRectMake(0, 0, self.view.XW_width, self.view.XW_high);
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(self.view.XW_width * 2, 0);
}
- (void)setUI{
    
    NSArray *segArr = @[@"趣味段子",@"搞笑图片"];
    self.seg = [[UISegmentedControl alloc] initWithItems:segArr];
    self.seg.tintColor = XWCommonColor;
    [self.seg setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:16]} forState:(UIControlStateSelected)];
    [self.seg setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15]} forState:(UIControlStateNormal)];
    _seg.frame = CGRectMake(0, 0, 200, 30);
    [_seg addTarget:self action:@selector(onClick:) forControlEvents:(UIControlEventValueChanged)];
    self.navigationItem.titleView = self.seg;
    self.seg.selectedSegmentIndex = 0;
    if (self.seg.selectedSegmentIndex==0) {
        [self onClick:_seg];
    }
}


-(void)addChildVCView:(NSInteger)index{
    CGFloat offsetX =index * KScreen_Width;
    
    UIViewController *childVc = self.childViewControllers[index];
    //如果加载直接返回
    if (childVc.isViewLoaded)return;

    childVc.view.frame = CGRectMake(offsetX, 0, KScrollViewWidth, KScrollViewHeight);
    
    [self.scrollView addSubview:childVc.view];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    self.seg.selectedSegmentIndex = scrollView.contentOffset.x/KScrollViewWidth+0.5;
    [self addChildVCView: self.seg.selectedSegmentIndex];
}



- (void)onClick:(UISegmentedControl *)segmentedControl{
   
    self.scrollView.contentOffset = CGPointMake(self.view.XW_width * self.seg.selectedSegmentIndex, 0.0f);

    NSInteger index = self.scrollView.contentOffset.x/self.view.bounds.size.width;
    
    [self addChildVCView:index];
}




@end

